#include "flag_ptn_ptn_ptn.h"
#include <QPushButton>
#include <QHBoxLayout>
#include <QJsonObject>
#include <QJsonValue>
#include <QDebug>
#include <QIcon>

/**********************************************
* @file       flag_ptn_ptn_ptn.cpp
* @brief
*       "tcp_ptn": {
        "flag_ptn_ptn_ptn": {
          "pushbutton1": {
            "name": "设置",
            "Commands": "set(x=<value>)"
          },
          "pushbutton2": {
            "name": "move",
            "Commands": "move(x=<value>)"
          },
          "pushbutton3": {
            "name": "IO",
            "Commands": "IO(x=<value>)"
          }
        }
      }
    },
*
* @author     yfk
* @date       2025-07-24
**********************************************/


Flag_Ptn_Ptn_Ptn::Flag_Ptn_Ptn_Ptn(const QJsonObject &flagDetails, QWidget *parent)
    : QWidget(parent)
{
    setupControlLayout(flagDetails);  // 设置控件的布局
}

QPushButton* Flag_Ptn_Ptn_Ptn::createStyledButton(const QString &text, const QString &iconPath)
{
    QPushButton *button = new QPushButton(text, this);
    button->setIcon(QIcon(iconPath));
    button->setIconSize(QSize(18, 18));
    button->setMinimumHeight(40);
    button->setCursor(Qt::PointingHandCursor);
    button->setLayoutDirection(Qt::LeftToRight);
    button->setContentsMargins(0, 0, 0, 0);

    QString style = R"(
        QPushButton {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                                        stop:0 #2196f3, stop:1 #1e88e5);
            color: white;
            border: none;
            padding: 4px 20px;
            border-radius: 8px;
            font-size: 20px;
            font-weight: bold;
            font-family: 'Microsoft YaHei';
            qproperty-iconSize: 20px;
            text-align: center;
        }
        QPushButton:hover {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:0,
                                        stop:0 #1e88e5, stop:1 #1565c0);
        }
    )";
    button->setStyleSheet(style);

    return button;
}

void Flag_Ptn_Ptn_Ptn::setupControlLayout(const QJsonObject &flagDetails)
{
    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(10);

    // 遍历 JSON 对象中的所有按钮
    for (const QString &key : flagDetails.keys()) {
        QJsonObject btnObj = flagDetails[key].toObject();
        QString btnText = btnObj["name"].toString();
        QString command = btnObj["Commands"].toString();

        // 图标路径可以根据名称或 key 动态决定（此处简单使用默认图标）
        QString iconPath = ":/img/fa-cog.png";  // 可根据 btnText 设置不同图标

        QPushButton *btn = createStyledButton(btnText, iconPath);
        layout->addWidget(btn);
        buttonCommandMap.insert(btnText, command);

        connect(btn, &QPushButton::clicked, this, &Flag_Ptn_Ptn_Ptn::onButtonClicked);
    }

    setLayout(layout);
}

void Flag_Ptn_Ptn_Ptn::onButtonClicked()
{
    QPushButton *button = qobject_cast<QPushButton*>(sender());
    if (button) {
        QString buttonText = button->text();
        qDebug() << "Button clicked: " << buttonText;

        if (buttonCommandMap.contains(buttonText)) {
            QString command = buttonCommandMap.value(buttonText);
            qDebug() << "Executing command: " << command;
            emit commandExecuted(command);
        } else {
            qWarning() << "No command found for button: " << buttonText;
        }
    }
}
